#include "includes.h"
#include <string.h> 
#include <stdio.h>  
#include <stdbool.h>
#include <IOdevice.h>
#include "xrj_protocol.h"
#include "Led.h"
//----------------LED------------------------------------------------------------------------------------

#define	GPIO_Pin_R_LED		GPIO_Pin_10
#define	GPIO_Pin_L_LED		GPIO_Pin_15

#define	P_Out_R_LED	 	PCout(10)
#define	P_Out_L_LED	 	PAout(15)



#define	GPIO_Pin_R_LIGHT	GPIO_Pin_0
#define	GPIO_Pin_L_LIGHT	GPIO_Pin_2

#define	P_Out_R_LIGHT	 PBout(0)
#define	P_Out_L_LIGHT	 PBout(2)

#define	GPIO_Pin_SW1	GPIO_Pin_4
#define	GPIO_Pin_SW2	GPIO_Pin_5

#define	P_Out_SW1	 	 PCout(4)
#define	P_Out_SW2		 PCout(5)

void LED_Init(void)
{			

	
	GPIO_InitTypeDef  GPIO_InitStructure; 
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC |RCC_APB2Periph_AFIO, ENABLE); //原来的初始化代码没有打开时钟 
	
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_R_LED|GPIO_Pin_SW1|GPIO_Pin_SW2; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_L_LED; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_R_LIGHT|GPIO_Pin_L_LIGHT; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     
	GPIO_Init(GPIOB, &GPIO_InitStructure);	
	
	

	P_Out_R_LED = 0;
	P_Out_L_LED = 0;
	P_Out_R_LIGHT = 0;
	P_Out_L_LIGHT = 0;
	P_Out_SW1 = 0;
	P_Out_SW2 = 0;
}

void LED_Open(UINT32 index)
{
	if (index == IOT__UNIT_INDEX__LED_1)
	{
		P_Out_R_LED = 1;
	}
	else if (index ==IOT__UNIT_INDEX__LED_2)
	{
		P_Out_L_LED = 1;
	}	
	else if (index == IOT__UNIT_INDEX__LIGHT_OUTSIDE_1)
	{
		P_Out_R_LIGHT = 1;
	}
	else if (index == IOT__UNIT_INDEX__LIGHT_OUTSIDE_2)
	{
		P_Out_L_LIGHT = 1;
	}	

	else if (index == IOT__UNIT_INDEX__LIGHT_INSIDE_2)
	{
		P_Out_SW2 = 1;
	}	
	
}	

void LED_Close(UINT32 index)
{
	if (index == IOT__UNIT_INDEX__LED_1)
	{
		P_Out_R_LED = 0;
	}
	else if (index ==IOT__UNIT_INDEX__LED_2)
	{
		P_Out_L_LED = 0;
	}	
	else if (index == IOT__UNIT_INDEX__LIGHT_OUTSIDE_1)
	{
		P_Out_R_LIGHT = 0;
	}
	else if (index == IOT__UNIT_INDEX__LIGHT_OUTSIDE_2)
	{
		P_Out_L_LIGHT = 0;
	}	

	else if (index == IOT__UNIT_INDEX__LIGHT_INSIDE_2)
	{
		P_Out_SW2 = 0;
	}		
}



void LED_Proc(void)
{	
	XRJ_mutex_lock(&Device_Value.Lock);
//--------------ON
	if ((Device_Value.LED_1_Setting == IOT__STATUS_CODES__ON) || (Device_Value.LED_1_Setting == IOT__STATUS_CODES__OFF)) 
	{	
		Device_Value.LED_1_Report = Device_Value.LED_1_Setting ;
		Device_Value.LED_1_Setting =  IOT__STATUS_CODES__NONE;
		if (Device_Value.LED_1_Report == IOT__STATUS_CODES__ON)
		{
			LED_Open(IOT__UNIT_INDEX__LED_1);
		}
		else
		{
			LED_Close(IOT__UNIT_INDEX__LED_1);
		}	
	}	

	if ((Device_Value.LED_2_Setting == IOT__STATUS_CODES__ON) || (Device_Value.LED_2_Setting == IOT__STATUS_CODES__OFF)) 
	{
		Device_Value.LED_2_Report = Device_Value.LED_2_Setting ;
		Device_Value.LED_2_Setting =  IOT__STATUS_CODES__NONE;
		if (Device_Value.LED_2_Report == IOT__STATUS_CODES__ON)
		{
			LED_Open(IOT__UNIT_INDEX__LED_2);
		}
		else
		{
			LED_Close(IOT__UNIT_INDEX__LED_2);
		}		
	}	
	
//	if ((Device_Value.LIGHT_INSIDE_1_Setting == IOT__STATUS_CODES__ON) || (Device_Value.LIGHT_INSIDE_1_Setting == IOT__STATUS_CODES__OFF))
//	{
//		Device_Value.LIGHT_INSIDE_1_Report = Device_Value.LIGHT_INSIDE_1_Setting ;
//		Device_Value.LIGHT_INSIDE_1_Setting =  IOT__STATUS_CODES__NONE;
//		if (Device_Value.LIGHT_INSIDE_1_Report == IOT__STATUS_CODES__ON)
//		{
//			LED_Open(IOT__UNIT_INDEX__LIGHT_INSIDE_1);
//		}
//		else
//		{
//			LED_Close(IOT__UNIT_INDEX__LIGHT_INSIDE_1);
//		}			
//	}	
	
	if ((Device_Value.LIGHT_INSIDE_2_Setting == IOT__STATUS_CODES__ON) || (Device_Value.LIGHT_INSIDE_2_Setting == IOT__STATUS_CODES__OFF))
	{
		Device_Value.LIGHT_INSIDE_2_Report = Device_Value.LIGHT_INSIDE_2_Setting ;
		Device_Value.LIGHT_INSIDE_2_Setting =  IOT__STATUS_CODES__NONE;
		if (Device_Value.LIGHT_INSIDE_2_Report == IOT__STATUS_CODES__ON)
		{
			LED_Open(IOT__UNIT_INDEX__LIGHT_INSIDE_2);
		}
		else
		{
			LED_Close(IOT__UNIT_INDEX__LIGHT_INSIDE_2);
		}			
	}		
	
	if ((Device_Value.LIGHT_OUTSIDE_1_Setting == IOT__STATUS_CODES__ON) || (Device_Value.LIGHT_OUTSIDE_1_Setting == IOT__STATUS_CODES__OFF))
	{
		Device_Value.LIGHT_OUTSIDE_1_Report = Device_Value.LIGHT_OUTSIDE_1_Setting ;
		Device_Value.LIGHT_OUTSIDE_1_Setting =  IOT__STATUS_CODES__NONE;
		if (Device_Value.LIGHT_OUTSIDE_1_Report == IOT__STATUS_CODES__ON)
		{
			LED_Open(IOT__UNIT_INDEX__LIGHT_OUTSIDE_1);
		}
		else
		{
			LED_Close(IOT__UNIT_INDEX__LIGHT_OUTSIDE_1);
		}			
	}	
	
	if ((Device_Value.LIGHT_OUTSIDE_2_Setting == IOT__STATUS_CODES__ON) || (Device_Value.LIGHT_OUTSIDE_2_Setting == IOT__STATUS_CODES__OFF))
	{
		Device_Value.LIGHT_OUTSIDE_2_Report = Device_Value.LIGHT_OUTSIDE_2_Setting ;
		Device_Value.LIGHT_OUTSIDE_2_Setting =  IOT__STATUS_CODES__NONE;
		if (Device_Value.LIGHT_OUTSIDE_2_Report == IOT__STATUS_CODES__ON)
		{
			LED_Open(IOT__UNIT_INDEX__LIGHT_OUTSIDE_2);
		}
		else
		{
			LED_Close(IOT__UNIT_INDEX__LIGHT_OUTSIDE_2);
		}			
	}		
	XRJ_mutex_unlock(&Device_Value.Lock);	
}









